/**
 * PollingUtil.java
 * Copyright 2014-10-6 FengXinZi,all right reserved.
 * FengXinZi PROPRIETARY/CONFIDENTIAL, any from of usage is subject to approval.
 */
/**
 *
 */
package com.ikangtai.shecare.common.util;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.SystemClock;
import android.util.Log;

import com.ikangtai.shecare.common.App;
import com.ikangtai.shecare.personal.SettingsActivity;

import java.util.Calendar;
import java.util.TimeZone;

/**
 *
 */
public class PollingUtil {
    public static final int NOTIFICATION_PLAN = 101;
//    public static final int NOTIFICATION_DAY = 102;

    // 开启轮询服务
    public static void startPollingService(final Context context, final int notificationType, final long intervalMills, final
    Class<?> cls, final String action) {


        AlarmManager manager = (AlarmManager) context
                .getSystemService(Context.ALARM_SERVICE);
        Intent intent = new Intent(context, cls);
        intent.setAction(action);
        PendingIntent pendingIntent = PendingIntent.getService(context, 0,
                intent, PendingIntent.FLAG_UPDATE_CURRENT);

        long triggerAtTime = SystemClock.elapsedRealtime();
        final long systemTime = System.currentTimeMillis();
        String settingTime = App.getPreference(SettingsActivity.kPlanNotiTime).equals("") ? "10:00" : App.getPreference(SettingsActivity.kPlanNotiTime);
//        String settingTime = (notificationType == NOTIFICATION_DAY) ? "10:00" : App.getPreference(SettingsActivity.kPlanNotiTime);
        final Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.setTimeZone(TimeZone.getTimeZone("GMT+8")); // 这里时区需要设置一下，不然会有8个小时的时间差
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(settingTime.split(":")[0]));
        calendar.set(Calendar.MINUTE, Integer.parseInt(settingTime.split(":")[1]));
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        String mWay = String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
        if ("1".equals(mWay)) {
            mWay = "Sun";
            if (!App.ALERT_REPEAT_SUN) {
                return;
            }
        } else if ("2".equals(mWay)) {
            mWay = "Mon";
            if (!App.ALERT_REPEAT_MON) {
                return;
            }
        } else if ("3".equals(mWay)) {
            mWay = "Tue";
            if (!App.ALERT_REPEAT_TUE) {
                return;
            }
        } else if ("4".equals(mWay)) {
            mWay = "Wed";

            if (!App.ALERT_REPEAT_WED) {
                return;
            }
        } else if ("5".equals(mWay)) {
            mWay = "Thu";
            if (!App.ALERT_REPEAT_THU) {
                return;
            }
        } else if ("6".equals(mWay)) {
            mWay = "Fri";

            if (!App.ALERT_REPEAT_FRI) {
                return;
            }
        } else if ("7".equals(mWay)) {
            mWay = "Sta";
            if (!App.ALERT_REPEAT_STA) {
                return;
            }
        }


        // 选择的每天定时时间
        long selectTime = calendar.getTimeInMillis();
        // 如果当前时间大于设置的时间，那么就从第二天的设定时间开始
        if (systemTime > selectTime) {
            calendar.add(Calendar.DAY_OF_MONTH, 1);
            selectTime = calendar.getTimeInMillis();
        }
        // 计算现在时间到设定时间的时间差
        final long time = selectTime - systemTime;
        triggerAtTime += time;
        manager.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime,
                intervalMills, pendingIntent);
    }

    // 停止轮询服务
    public static void stopPollingService(final Context context, final int notificationType, final Class<?> cls, final String
            action) {
        final AlarmManager manager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        final Intent intent = new Intent(context, cls);
        intent.setAction(action);
        final PendingIntent pendingIntent = PendingIntent.getService(context, notificationType, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);
        // 取消正在执行的服务
        manager.cancel(pendingIntent);
    }
}
